package net.sparrow;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

import net.minecraft.src.Minecraft;
import net.minecraft.src.Session;
import net.sparrow.cmd.CommandService;
import net.sparrow.data.DataService;
import net.sparrow.data.UtilFile;
import net.sparrow.event.DataField;
import net.sparrow.event.SpEventListener;
import net.sparrow.event.SpEvent;
import net.sparrow.event.SpKeyboardEvent;
import net.sparrow.event.SpShutdownEvent;
import net.sparrow.event.SpStartupEvent;
import net.sparrow.event.SpTickEvent;
import net.sparrow.gui.GuiService;
import net.sparrow.gui.drawing.SpColor;
import net.sparrow.mcaccess.MinecraftAccessService;
import net.sparrow.mcaccess.ReiAccessService;
import net.sparrow.module.Module;
import net.sparrow.module.StartupListenerModule;
import net.sparrow.util.Util;

public class Sparrow {

	//this is a singleton
	private static final Sparrow instance = new Sparrow();

	//shutdown: MainShutdownHook.java after integrated server
	//statup: Minecraft.java, last line in constructor
	//RendererLivingEntity.java:func_130000_a:before renderModel
	
	
	
	///HOW TO EXPORT
	//remove 1.6.2.jar from ref libraries
	//client -> export -> runnable jar file
	//open up exported jar
	//add pack.mcmeta, pack.png,font.txt, achievement, assets folder from 1.6.2.jar to root folder	(same dir as start.class)
	
	//TODO fix this stupid build stuff^
	////Services
	private static final DataService dataService = new DataService();
	private static final GuiService guiService =  new GuiService();
	private static final CommandService commandService = new CommandService();
	private static final MinecraftAccessService minecraftService = new MinecraftAccessService();
	private static final ReiAccessService reiService = new ReiAccessService();
	
	public static int lastKey = -1;
	public static Long lastKeyUpdate = System.currentTimeMillis();
	public static final long KEY_DELAY = 500l; //ms

	public static Sparrow get() {
		return instance; // soham likes men only
	}

	public static DataService data() {
		return dataService;
	}
	
	public static GuiService gui() {
		return guiService;
	}
	
	public static CommandService cmds() {
		return commandService;
	}
	
	public static ReiAccessService minimap() {
		return reiService;
	}
	
	public static MinecraftAccessService mc() {
		return minecraftService;
	}

	public void onStartup() {
		UtilFile.setupPrintFile();
		Util.print("Sparrow Client: Starting. " + ((new Date()).toString()));
		//Display.setIcon
		Display.setTitle("Sparrow 1.6.2");
		//TODO this is just debug code
		String user = JOptionPane.showInputDialog("Username?");
		String pass = JOptionPane.showInputDialog("Pass?");
		Sparrow.mc().minecraft().session = new Session(user,"111111");
		if(!user.equals("x")) {
			Util.print("Logging in as '" + user + "'");
			Util.login(user,pass);	
		}
		Module.loadModules();
		UtilFile.loadData();
		UtilFile.loadKeybinds();
		SpColor.loadColors();
		fireEvent(new SpStartupEvent());
	}
	
	public void onShutdown() {
		fireEvent(new SpShutdownEvent());
		UtilFile.saveData();
		UtilFile.saveKeybinds();
		UtilFile.closePrintFile();
	}

	public void onTick() {
		Sparrow.get().fireEvent(new SpTickEvent());
		
		minimap().forceEntityRadar();
		if (System.currentTimeMillis() - lastKeyUpdate > KEY_DELAY) { //reset key state, allow a repeat event
			lastKey = -1;
		}
		if(Keyboard.getEventKeyState() && !Keyboard.isRepeatEvent() && (Keyboard.getEventKey() != lastKey)) {
			fireEvent(new SpKeyboardEvent(Keyboard.getEventCharacter(),Keyboard.getEventKey()));
			lastKey = Keyboard.getEventKey();
			lastKeyUpdate = System.currentTimeMillis();
			
			//if this key is a keybound one, execute the cmd
			for( String key : Sparrow.data().getKeybindKeys()) {
				String eventKeyName = Keyboard.getKeyName(Keyboard.getEventKey());
				if (key.equals(eventKeyName)) {
					Util.debug("Executing Keybind " + eventKeyName + ": '" + (Sparrow.data().getKeybindCommand(eventKeyName)));
					Sparrow.cmds().executeCommand(Sparrow.data().getKeybindCommand(eventKeyName));
				}
			}
		}
	}
	
	public void fireEvent(SpEvent e) {
		data().fireEvent(e);
	}

}
